<?php 

class wxShareModel extends appModel
{
    
    /**
     * 判断是否已经存在相同的标识
     * @param $code;标识
     * @param string $id;数据id
     * @return 数量
     */
    public function checkCodeExist($code, $id = '')
    {
        $where = " WHERE share_code='".$code."' ";
        if(!empty($id))
        {
            $where .= " AND id<>'".$id."' ";
        }
        $sql = " SELECT COUNT(*) FROM ".$this->table('wxshare').$where;
        return $this->getOne($sql);
    }
    
    /**
     * 插入分享信息
     * @param $info;分享数据
     * @return 状态
     */
    public function insertShare($info)
    {
        $data = yf_set_insert($info);
        $sql = " INSERT INTO ".$this->table('wxshare')." ( ".$data['key']." ) VALUES( ".$data['val']." ) ";
        return $this->query($sql);
    }
    
    /**
     * 获取分享列表
     * @param $page
     * @param number $limit
     * @return 数据
     */
    public function getShareList($page, $limit = 10)
    {
        $sql = " SELECT * FROM ".$this->table("wxshare")." ORDER BY created DESC ";
        $data = $this->show_page($sql, $page, $limit);
        if($data['result'])
        {
            foreach($data['result'] as $k => $v)
            {
                $data['result'][$k]['created'] = date('Y-m-d H:i', $v['created']);
            }
        }
        return $data;
    }
    
    /**
     * 获取分享信息
     * @param $id
     * @return Ambigous <boolean, multitype:>
     */
    public function getShareInfo($id)
    {
        $sql = " SELECT * FROM ".$this->table("wxshare")." WHERE id='".$id."' ";
        return $this->getRow($sql);
    }
    
    /**
     * 修改分享信息
     * @param $info
     * @param $id
     * @return Ambigous <boolean, resource>
     */
    public function updateShare($info, $id)
    {
        $data = yf_set_update($info);
        $sql = " UPDATE ".$this->table("wxshare")." SET ".$data." WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
    public function delShare($id)
    {
        $sql = " DELETE FROM ".$this->table('wxshare')." WHERE id='".$id."' ";
        return $this->query($sql);
    }
    
}